/**
 * 动态api服务接口
 * @author lian
 */

import { DynamicApi } from '@/common/constants/request-api'
import { Method } from '@/common/enum/request-methods'
import type { CreateDynamicParams, Dynamic, DynamicComment, DynamicCreateParams, DynamicDetail } from '@/common/types/dynamic'
import type { PagedParams, PagedResult } from '@/common/types/request'

/**
 * 分页获取动态
 * @param pagedParams 分页参数
 * @author lian
 */
export const getPagedActivityService = (pagedParams: PagedParams) => {
  return useRequest<PagedResult<Dynamic[]>>({
    method: Method.GET,
    url: DynamicApi.PAGED_GET_DYNAMICS,
    data: {
      ...pagedParams,
    },
  })
}

/**
 * 根据活动id获取活动详情
 * @param dynamicId 动态id
 * @author lian
 */
export const getDynamicDetailByIdService = (dynamicId: number) => {
  const url = replaceParams(DynamicApi.GET_DYNAMIC_DETAIL, [
    {
      paramName: 'id',
      value: dynamicId,
    },
  ])

  return useRequest<DynamicDetail>({
    method: Method.GET,
    url,
  })
}

/**
 * 分页获取动态的评论
 * @param pagedParams 分页评论
 * @param dynamicId 动态id
 * @author lian
 */
export const getPagedDynamicCommentByIdService = (pagedParams: PagedParams, dynamicId: number) => {
  const url = replaceParams(DynamicApi.PAGED_GET_DYNAMIC_COMMENTS, [
    {
      paramName: 'id',
      value: dynamicId,
    },
  ])

  return useRequest<PagedResult<DynamicComment[]>>({
    method: Method.GET,
    url,
    data: {
      ...pagedParams,
    },
  })
}

/**
 * 创建动态
 * @param data 动态信息
 * @author lian
 */
export const createDynamicService = (data: CreateDynamicParams) => {
  return useRequest({
    method: Method.POST,
    url: DynamicApi.CREATE_DYNAMIC,
    data,
  })
}

/**
 * 创建动态评论
 * @param dynamicId 动态id
 * @param data 评论信息
 * @author lian

 */
export const createDynamicCommentService = (dynamicId: number, data: DynamicCreateParams) => {
  const url = replaceParams(DynamicApi.CREATE_DYNAMIC_COMMENT, [
    {
      paramName: 'id',
      value: dynamicId,
    },
  ])

  return useRequest({
    method: Method.POST,
    url,
    data,
  })
}

/**
 * 创建动态评论
 * @param dynamicId 动态id
 * @author lian
 */
export const dynamicLikeService = (dynamicId: number) => {
  const url = replaceParams(DynamicApi.LIKE_DYNAMIC, [
    {
      paramName: 'id',
      value: dynamicId,
    },
  ])

  return useRequest({
    method: Method.POST,
    url,
  })
}

/**
 * 取消点赞
 * @param dynamicId 动态id
 * @returns boolean
 * @author lian
 */
export const dynamicCancelLikeService = (dynamicId: number) => {
  const url = replaceParams(DynamicApi.CANCEL_LIKE_DYNAMIC, [
    {
      paramName: 'id',
      value: dynamicId,
    },
  ])
  return useRequest({
    method: Method.DELETE,
    url,
  })
}

/**
 * 查询当前登录用户是否已点赞该动态
 * @param dynamicId 动态id
 * @author lian
 */
export const checkUserLikeDynamicService = (dynamicId: number) => {
  const url = replaceParams(DynamicApi.IS_LIKE_DYNAMIC, [
    {
      paramName: 'id',
      value: dynamicId,
    },
  ])

  return useRequest<boolean>({
    method: Method.GET,
    url,
  })
}
